package com.project.controller.web;

import com.project.common.utils.PublicUtils;
import com.project.entity.*;
import com.project.myBeans.ExamunblankAnswer;
import com.project.myBeans.ExerciseDetail;
import com.project.myBeans.R;
import com.project.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/6/6 17:06
 * @param:
 * @return:
 */
@EnableAutoConfiguration
@RestController
@Api(description = "考试答案记录表", value = "examanswerrecordcontroller")
@RequestMapping("/examanswerrecordcontroller")
public class ExamAnswerRecordController {
    @Autowired
    ExamRecordService examRecordService;

    @Autowired
    ExamBlankrecordService examBlankrecordService;

    @Autowired
    AnswerRecordService answerRecordService;

    @Autowired
    CompletionService completionService;
    @Autowired
    CompAnswerService compAnswerService;

    @Autowired
    ExerciseInfoService exerciseInfoService;
    /**
     * Tate 2018/6/6
     *
     * @param examunblankAnswer
     * @return
     */
    @ApiOperation("非填空题题目答案记录")
    @PostMapping("/AddorModifyExerciserecord")
    public R AddorModifyExerciserecord(@RequestBody ExamunblankAnswer examunblankAnswer) {
        //如果答案未记录
        try {
            if (answerRecordService.findbythree(examunblankAnswer.getAnswerRecord().getStudentid(), examunblankAnswer.getAnswerRecord().getExamid(), examunblankAnswer.getAnswerRecord().getExerciseinfoid()).isEmpty()) {
                answerRecordService.add(examunblankAnswer.getAnswerRecord());
                ExamRecord examRecord = new ExamRecord();
                AnswerRecord tmp = answerRecordService.findbythree(examunblankAnswer.getAnswerRecord().getStudentid(), examunblankAnswer.getAnswerRecord().getExamid(), examunblankAnswer.getAnswerRecord().getExerciseinfoid()).get(0);
                examRecord.setAnswerrecordid(tmp.getId());
                examRecord.setAnswer(examunblankAnswer.getAnswer());
                examRecord.setIstrue(1);
                float tttt = 0;
                examRecord.setScore(tttt);
                examRecordService.add(examRecord);
                return R.ok("保存成功");
            } else {
                ExamRecord examRecord = new ExamRecord();
                AnswerRecord tmp = answerRecordService.findbythree(examunblankAnswer.getAnswerRecord().getStudentid(), examunblankAnswer.getAnswerRecord().getExamid(), examunblankAnswer.getAnswerRecord().getExerciseinfoid()).get(0);
                examRecord.setAnswerrecordid(tmp.getId());
                examRecord.setAnswer(examunblankAnswer.getAnswer());
                examRecordService.modify(examRecord);
                return R.ok("修改成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }

    /**
     * Tate 2018/6/6
     *
     * @param examunblankAnswer
     * @return
     */
    @ApiOperation("填空题题目答案记录")
    @PostMapping("/AddorModifyBlankrecord")
    public R AddorModifyBlankrecord(@RequestBody ExamunblankAnswer examunblankAnswer) {
        try {
            if (answerRecordService.findbythree(examunblankAnswer.getAnswerRecord().getStudentid(), examunblankAnswer.getAnswerRecord().getExamid(), examunblankAnswer.getAnswerRecord().getExerciseinfoid()).isEmpty()) {
                answerRecordService.add(examunblankAnswer.getAnswerRecord());
                List<String> ty = PublicUtils.getStringList(examunblankAnswer.getAnswer(), "`");
                AnswerRecord tmp = answerRecordService.findbythree(examunblankAnswer.getAnswerRecord().getStudentid(), examunblankAnswer.getAnswerRecord().getExamid(), examunblankAnswer.getAnswerRecord().getExerciseinfoid()).get(0);
                for (int i = 0; i < ty.size(); i++) {
                    ExamBlankrecord examBlankrecord = new ExamBlankrecord();
                    examBlankrecord.setAnswerrecordid(tmp.getId());
                    examBlankrecord.setAnswer(ty.get(i));
                    examBlankrecord.setIndex(i + 1);
                    examBlankrecord.setIstrue(1);
                    float tttt = 0;
                    examBlankrecord.setScore(tttt);
                    examBlankrecordService.add(examBlankrecord);
                }
                return R.ok("添加成功");
            } else {
                List<String> ty = PublicUtils.getStringList(examunblankAnswer.getAnswer(), "`");
                AnswerRecord tmp = answerRecordService.findbythree(examunblankAnswer.getAnswerRecord().getStudentid(), examunblankAnswer.getAnswerRecord().getExamid(), examunblankAnswer.getAnswerRecord().getExerciseinfoid()).get(0);
                examBlankrecordService.delete(tmp.getId());
                for (int i = 0; i < ty.size(); i++) {
                    ExamBlankrecord examBlankrecord = new ExamBlankrecord();
                    examBlankrecord.setAnswerrecordid(tmp.getId());
                    examBlankrecord.setAnswer(ty.get(i));
                    examBlankrecord.setIndex(i + 1);
                    examBlankrecord.setIstrue(1);
                    float tttt = 0;
                    examBlankrecord.setScore(tttt);
                    examBlankrecordService.add(examBlankrecord);
                }
                return R.ok("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * @author: DragonDoor
     * @Date: 2018/10/31 14:17
     * @Description: 考试答案保存
     */
    @ApiOperation("考试答案保存")
    @PostMapping("/saveExamAnswer/{examid}/{studentid}")
    public R saveExamAnswer(@RequestBody List<ExerciseDetail> exerciseDetails,@PathVariable Integer examid,@PathVariable Integer studentid)
    {
        try
        {
            answerRecordService.saveExamAnswer(exerciseDetails,examid,studentid);
            return R.ok();
        }catch(Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * @author: DragonDoor
     * @Date: 2018/10/31 15:41
     * @Description:
     */
//    @ApiOperation("返回学生试卷与答案")
//    @PostMapping("/showPage")
//    public R showPage(@RequestBody AttendExam attendExam)
//    {
//        answerRecordService.
//    }


    /**
     * Tate 2018/6/6
     *
     * @param id
     * @return
     */
    @ApiOperation("根据填空题题目id返回题目数目")
    @GetMapping("/FindNumbyid/{id}")
    public R FindNumbyid(@PathVariable Integer id) {
        try {
            Completion completion = completionService.findByExerciseInfoId(id);
            String[] list = compAnswerService.findByCompidOrderByIndex(completion.getId());
            return R.ok("返回成功").put("data", list.length);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Tate 2018/6/6
     *
     * @param answerRecord
     * @return
     */
    
    /**
     * @author: DragonDoor
     * @Date: 2018/11/1 17:00
     * @Description: 修改
     */
    @ApiOperation("根据题目情况返回答案情况")
    @PostMapping("/FindAnserbythreeids")
    public R FindAnserbythreeids(@RequestBody AnswerRecord answerRecord) {
        try {
            if (answerRecordService.findbythree(answerRecord.getStudentid(), answerRecord.getExamid(), answerRecord.getExerciseinfoid()).isEmpty()) {
                return R.ok().put("data", null);
            } else {
                if (exerciseInfoService.findByPrimaryKey(answerRecord.getExerciseinfoid()).getType() != 4) {
                    AnswerRecord tmp = answerRecordService.findbythree(answerRecord.getStudentid(), answerRecord.getExamid(), answerRecord.getExerciseinfoid()).get(0);
                    ExamRecord examRecord = examRecordService.findbyAnswerrecordid(tmp.getId());
                    return R.ok("返回答案成功！").put("data", examRecord);
                } else {
                    AnswerRecord tmp = answerRecordService.findbythree(answerRecord.getStudentid(), answerRecord.getExamid(), answerRecord.getExerciseinfoid()).get(0);
                    List<ExamBlankrecord> examBlankrecords = examBlankrecordService.findbyAnswerrecordid(tmp.getId());
                    return R.ok("返回答案成功").put("data", examBlankrecords);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
}
